<?php
/**
 * [商网通 System] Copyright swtshop.cn
 * QQ: 1170787711
 * Author: gj.name
 * This is NOT a freeware
*/
defined('IN_UQ') or exit('Access Denied');
class bill_uqcms extends control
{
    function __construct()
    {
        $this->aid = $_SESSION['admin']['aid'];
        parent::__construct();
    }
    public function index()
    {
        $page = isset($_GET['page']) ? $_GET['page'] : '1';
        $page_size = isset($_GET['page_size']) ? $_GET['page_size'] : '20';
        $wsql = '';
        $pay_status = isset($_GET['pay_status']) ? $_GET['pay_status'] : '';
        if ($pay_status) {
            $wsql = ' pay_status = ' . $pay_status . ' and ';
        } elseif ($pay_status == '0') {
            $wsql = ' pay_status = 0 and ';
        }
        $key = isset($_GET['key']) ? $_GET['key'] : '';
        if (!empty($key)) {
            $wsql = " trade_no =  '" . $key . "'";
        }
        if (!empty($wsql)) {
            $wsql = ' where ' . rtrim($wsql, ' and ');
        }
        $rall = $this->db->get_all("select * from " . table('admin_pay') . $wsql . " order by id desc  limit " . ($page - 1) * $page_size . "," . $page_size);
        if ($rall) {
            foreach ($rall as $k => $v) {
                $v['pay_time'] = _date($v['pay_time']);
                $v['addtime'] = _date($v['addtime']);
                $list[] = $v;
            }
            $data['list'] = $list;
            $total = $this->db->get_one("select count(id) as total from " . table('admin_pay') . $wsql);
            if ($total['total'] > $page_size) {
                $data['page'] = $this->uqcms_page(array('page' => $page, 'page_size' => $page_size, 'total' => $total['total']));
            }
            $this->assign('data', $data);
        }
        $this->display();
    }
    public function pay_detail()
    {
        $shop_num = $this->db->get_one("select count(id) as total from " . table("shop"));
        $this->assign("shop_num", $shop_num['total']);
        $goods_num = $this->db->get_one("select count(id) as total from " . table('goods'));
        $this->assign("goods_num", $goods_num['total']);
        $total = $this->db->get_one("select count(id) as total from " . table('admin_pay'));
        $this->assign("admin_pay_num", $total['total']);
        $this->display();
    }
    public function withdraw()
    {
        $page = isset($_GET['page']) ? $_GET['page'] : '1';
        $page_size = isset($_GET['page_size']) ? $_GET['page_size'] : '15';
        $pay_status = isset($_GET['pay_status']) ? $_GET['pay_status'] : '';
        $wsql = '';
        if (!empty($pay_status)) {
            $wsql .= ' pay_status = ' . $pay_status;
        }
        if (!empty($wsql)) {
            $wsql = ' where ' . rtrim($wsql, ' and ');
        }
        $rall = $this->db->get_all("select * from " . table('member_withdraw') . $wsql . " order by id desc limit " . ($page - 1) * $page_size . "," . $page_size);
        if ($rall) {
            foreach ($rall as $v) {
                if ($v['uid'] != '0') {
                    $u_arr[] = $v['uid'];
                } else {
                    $s_arr[] = $v['spid'];
                }
            }
            if (isset($u_arr)) {
                $uid_in = implode(',', $u_arr);
                $uquery = $this->db->get_all("select uid,mobile from " . table("member") . " where uid in(" . $uid_in . ")");
                if ($uquery) {
                    foreach ($uquery as $v) {
                        $member_arr[$v['uid']] = $v['mobile'];
                    }
                }
            }
            if (isset($s_arr)) {
                $spid_in = implode(',', $s_arr);
                $uquery = $this->db->get_all("select id,name from " . table("shop") . " where id in(" . $spid_in . ")");
                if ($uquery) {
                    foreach ($uquery as $v) {
                        $shop_arr[$v['id']] = $v['name'];
                    }
                }
            }
            foreach ($rall as $k => $v) {
                if ($v['spid'] != '0') {
                    if (isset($shop_arr[$v['spid']])) {
                        $v['username'] = $shop_arr[$v['spid']];
                    }
                } else {
                    if (isset($member_arr[$v['uid']])) {
                        $v['username'] = $member_arr[$v['uid']];
                    }
                }
                $v['pay_time'] = _date($v['pay_time']);
                $v['addtime'] = _date($v['addtime']);
                $list[] = $v;
            }
            $data['list'] = $list;
            $total = $this->db->get_one("select count(id) as total from " . table('member_withdraw') . $wsql);
            if ($total['total'] > $page_size) {
                $data['page'] = $this->uqcms_page(array('page' => $page, 'page_size' => $page_size, 'total' => $total['total']));
            }
            $this->assign('data', $data);
        }
        $this->display();
    }
    public function withdraw_do()
    {
        $id = isset($_GET['id']) ? $_GET['id'] : showmsg('参数错误');
        $row = $this->db->get_one("select * from " . table('member_withdraw') . " where id = " . $id);
        $this->assign('row', $row);
        $urow = $this->db->get_one("select uid,email,mobile from " . table('member') . " where uid = " . $row['uid']);
        $this->assign('urow', $urow);
        $this->display();
    }
    function withdraw_save()
    {
        if ($_POST) {
            $pay_status = P('pay_status', '', '1');
            if ($pay_status == '1') {
                $setsql['pay_status'] = '1';
                $setsql['pay_time'] = time();
                $setsql['pay_admin_code'] = P('pay_admin_code');
                $setsql['pay_admin_remark'] = P('pay_admin_remark');
            } elseif ($pay_status == '2') {
                $setsql['pay_status'] = '2';
                $setsql['refuse_time'] = time();
                $setsql['refuse_reason'] = P('refuse_reason');
            } else {
                error_json('提交错误');
            }
            $id = P('id', 0);
            if ($id) {
                $query = $this->db->update(table('member_withdraw'), $setsql, 'id = ' . $id);
                if ($query) {
                    right_json();
                } else {
                    error_json('操作失败');
                }
            } else {
                error_json('提交错误');
            }
        } else {
            error_json('提交错误');
        }
    }
    function withdraw_del()
    {
        $id = isset($_GET['id']) ? $_GET['id'] : error_json('提交错误');
        $query = $this->db->delete(table('member_withdraw'), 'spid = ' . $this->spid . " and id = " . $id);
        if ($query) {
            right_json();
        } else {
            error_json('删除错误');
        }
    }
}